package net.orcspace.myplace;

import java.util.List;

import net.orcspace.myplace.data.DataProvider;
import net.orcspace.myplace.google.GoogleDataProvider;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.appengine.api.oauth.OAuthRequestException;
import com.google.appengine.api.oauth.OAuthService;
import com.google.appengine.api.oauth.OAuthServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * This is the home controller of spring
 * @author Sun
 * @since 1.0
 */
@Controller
public class Home {
	
	@RequestMapping(value = "/")
	public String home(Model model){
		DataProvider.initProvider(1);
		if(isLogin()){
			//list todos
			List todos = DataProvider.p.listAllTodo();
			if(todos!=null && todos.size()>0)
			model.addAttribute("allTodos", todos);
			return "home";
		}else{
			DataProvider.initProvider(1);
			return "index";
		}
	}
	
	private boolean isLogin(){
		OAuthService oauth = OAuthServiceFactory.getOAuthService();
        try {
			User user = oauth.getCurrentUser();
			if(user==null)return false;
		} catch (OAuthRequestException e) {
			e.printStackTrace();
			return false;
		}
        return true;
	}
	

}
